//
//  NSMutableArray+Crash.m
//  flutter
//
//  Created by GilesZhou on 2020/6/29.
//  Copyright © 2020 Ganee. All rights reserved.
//

#import "NSMutableArray+Crash.h"
#import "CrashManager.h"

@implementation NSMutableArray (Crash)

+ (void)ch_observeCrash {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 类方法
        [CrashManager exchangeClassMethod:[self class] method1Sel:@selector(initWithObjects:count:) method2Sel:@selector(ch_initWithObjects:count:)];
        
        
        // 实例方法
        Class __NSArrayM = NSClassFromString(@"__NSArrayM");
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(objectAtIndex:) method2Sel:@selector(ch_objectAtIndex:)];
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(objectAtIndexedSubscript:) method2Sel:@selector(ch_objectAtIndexedSubscript:)];
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(insertObject:atIndex:) method2Sel:@selector(ch_insertObject:atIndex:)];
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(setObject:atIndexedSubscript:) method2Sel:@selector(ch_setObject:atIndexedSubscript:)];
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(removeObjectsInRange:) method2Sel:@selector(ch_removeObjectsInRange:)];
        
        [CrashManager exchangeInstanceMethod:__NSArrayM method1Sel:@selector(removeObjectAtIndex:) method2Sel:@selector(ch_removeObjectAtIndex:)];
        
    });
}

- (id)ch_objectAtIndex:(NSUInteger)index {
    id objectType = nil;
    
    @try {
        objectType = [self ch_objectAtIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return objectType;
    }
    
}

- (void)ch_insertObject:(id)anObject atIndex:(NSUInteger)index {
    
    @try {
        [self ch_insertObject:anObject atIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return;
    }
}

- (id)ch_objectAtIndexedSubscript:(NSUInteger)idx {
    id objectType = nil;
    
    @try {
        objectType = [self ch_objectAtIndexedSubscript:idx];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return objectType;
    }
}

- (void)ch_setObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
    
    @try {
        [self ch_setObject:obj atIndexedSubscript:idx];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return;
    }
}

- (void)ch_removeObjectsInRange:(NSRange)range {
    
    @try {
        [self ch_removeObjectsInRange:range];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return;
    }
}

- (void)ch_removeObjectAtIndex:(NSUInteger)index {
    
    @try {
        [self ch_removeObjectAtIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return;
    }
}

+ (instancetype)ch_initWithObjects:(const id  _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
    id instance = nil;
    
    @try {
        instance = [self ch_initWithObjects:objects count:cnt];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return instance;
    }
}

@end
